<template>
    <div class="page main">
        <el-container>
            <SideBar />
            <el-container class="layout">
                <el-header>
                    <Header />
                </el-header>
                <el-main>
                    <div class="content">
                        <router-view />
                    </div>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script setup>
import { storeToRefs } from "pinia";
import { useRouter } from "vue-router";
import { onMounted } from "vue";
import SideBar from "./components/sidebar";
import Header from "./components/header";
import { useUser } from "../../store/store";
import { GetUser } from "../../api";
import { GetStorage, SetStorage, ClearStorage } from "../../utils";
const router = useRouter();
const { user } = storeToRefs(useUser());
// 获取用户信息
const getUser = async () => {
    const { _id } = GetStorage("user");
    if (!_id) {
        router.replace("/login");
    }
    const res = await GetUser(_id);
    if (!res) {
        ClearStorage("user");
        router.replace("/login");
        return false;
    }
    SetStorage("user", res);
    user.value = res;
};

onMounted(() => {
    getUser();
});
</script>

<style lang="scss" scoped>
.header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
}
.el-header,
.el-main {
    padding-right: 0;
}
.content {
    background: #fff;
    height: 100%;
    padding: 24px;
    box-sizing: border-box;
}
</style>
